package JZOffer; /**
 * 剑指 Offer 26. 树的子结构
 * 输入两棵二叉树A和B，判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
 * B是A的子结构， 即 A中有出现和B相同的结构和节点值。
 * **/

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution26 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if(A == null || B == null) return false;
        return helper(A, B) || isSubStructure(A.left, B)||isSubStructure(A.right, B);
    }

    //判断当前树与目标树结构是否相同
    public boolean helper(TreeNode A, TreeNode B){
        if (B == null) return true;
        if (A == null) return false;
        return A.val == B.val && helper(A.left, B.left) && helper(A.right,B.right);
    }
}